using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Diagnostics;
using System.Net.Mail;
using System.Web;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace InfraStructureLayer
{
    [Serializable()]
    public class SFException : Exception
    {
        public override string Message
        {
            get
            {
                if (this.msgToUser == null || this.msgToUser == string.Empty)
                {
                    return this.msg;
                }
                else
                {
                    return this.msgToUser;
                }
            }
        }
        protected string msg;
        protected string msgToUser = string.Empty;
        protected string mesajId = "";
        public SFException()
        {
          
        }
        public SFException(string message)
            : base(message)
        {
            this.msg = message;
        }
        protected SFException(SerializationInfo info,StreamingContext context)
            : base(info,context)
        {
            
        }
        protected virtual void WriteExceptionToDb()
        {
            try
            {
                DbProviderFactory insDbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");
                DbConnection memConnection = insDbProviderFactory.CreateConnection();
                
                memConnection.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["SQLConnection"];

                DbCommand insDbCommand = insDbProviderFactory.CreateCommand();
                insDbCommand.CommandText = "InsertExceptions";
                insDbCommand.CommandType = System.Data.CommandType.StoredProcedure;
                insDbCommand.Connection = memConnection;


                DbParameter insDbParameter = insDbProviderFactory.CreateParameter();
                insDbParameter.ParameterName = "@pExceptionMessage";
                insDbParameter.Value = this.msg;
                insDbCommand.Parameters.Add(insDbParameter);

                DbParameter insDbParameter1 = insDbProviderFactory.CreateParameter();
                insDbParameter1.ParameterName = "@pExceptionSource";
                if (this.Source == null)
                {
                    insDbParameter1.Value = DBNull.Value;
                }
                else
                {
                    insDbParameter1.Value = this.Source;
                }
                insDbCommand.Parameters.Add(insDbParameter1);

                DbParameter insDbParameter2 = insDbProviderFactory.CreateParameter();
                insDbParameter2.ParameterName = "@pInnerExceptionMessage";
                if (this.InnerException == null)
                {
                    insDbParameter2.Value = DBNull.Value;
                }
                else
                {
                    insDbParameter2.Value = this.InnerException.ToString();
                }
                insDbCommand.Parameters.Add(insDbParameter2);

                DbParameter insDbParameter3 = insDbProviderFactory.CreateParameter();
                insDbParameter3.ParameterName = "@pExceptionDate";

                insDbParameter3.Value = DateTime.Now;
                insDbCommand.Parameters.Add(insDbParameter3);

                DbParameter insDbParameter5 = insDbProviderFactory.CreateParameter();
                insDbParameter5.ParameterName = "@pExceptionId";
                insDbParameter5.Direction = System.Data.ParameterDirection.Output;
                insDbParameter5.DbType = System.Data.DbType.Int32;
                insDbCommand.Parameters.Add(insDbParameter5);


                memConnection.Open();
                insDbCommand.ExecuteNonQuery();
                memConnection.Close();
                mesajId = insDbParameter5.Value.ToString();
                this.msgToUser += "\r\n Hata Kodu : " + insDbParameter5.Value.ToString();
            }
            catch (Exception ex)
            {
                

            }




        }
        protected virtual void SendEmail()
        {

          

        }

        

    }
}
